<script type="text/javascript">
    // 加载用户购物券
	function getUserCoupons(userId, goodsTotal){
		$.ajax({
			url : __contextPath + "/order/getUserCoupons.xhtml",
			type : "POST",
			data : {
				userId : userId,
				goodsTotal: goodsTotal
			},
			cache : false,
			dataType : 'json',
			success : function(data) {
				createCouponItems(data);
			},
			error : function(jqXHR, textStatus, errorThrown) {
				alert(jqXHR.responseText);
			}
		});
	}
	
	// 创建购物券项
	function createCouponItems(data){
		if(data == null || data == ""){
			$("#couponDiv1").hide();
			$("#couponDiv2").hide();
			return;
		}
		
		var tb = $("#couponTb");
		for(var i = 0; i < data.length; i++){
			var tr = $("<tr></tr>");
			var td1 = $("<td bgcolor='#ffffff'></td>");
			var td2 = $("<td bgcolor='#ffffff'></td>");
			var td3 = $("<td bgcolor='#ffffff'></td>");
			var td4 = $("<td bgcolor='#ffffff' align='center'></td>");
			
			td1.append(data[i][3]);
			td2.append("￥" + data[i][4]);
			td3.append(data[i][5]);
			td4.append($("<input type='radio' id=" + data[i][0] + " name="+ data[i][1] +" value=" + data[i][2] + " onclick='modifyOrderPrice(this, "+ data[i][4] +");'/>"));
			
			tr.append(td1);
			tr.append(td2);
			tr.append(td3);
			tr.append(td4);
			
			tb.append(tr);
		}
		
		var tr_e = $("<tr></tr>");
		var td_e = $("<td bgcolor='#ffffff' style='color:red;' colspan='4'>注意：购物券一次最多只能使用一张</td>");
		tr_e.append(td_e);
		tb.append(tr_e);
	}
	
	// 修改订单总价
	function modifyOrderPrice(obj, price){
		if(obj.checked){
			$("#couponTr").show();
			$("#btn_coupon").val(price);
			$("#btn_coupon_id").val(obj.value);
			$("#couponPrice").html(price);
			$("#payTotal").html(parseFloat($("#goodsTotal").html()) + parseFloat($("#postage").html()) - parseFloat(price));
		}
	}
	
	// 初始化页面数据
	function initTotal(){
		var totalPrice = 0;
		var totalMarket = 0;
		var disparity = 0;
		var percent = 0;
		var market = document.getElementsByName("marketPrice");
		var price = document.getElementsByName("goodsPrice");
		var num = document.getElementsByName("goodsNum");
		
		for (var i = 0, j = market.length; i < j; i++){
		    totalPrice += parseFloat(price[i].innerHTML) * parseInt(num[i].innerHTML);
		    totalMarket += (parseFloat(market[i].innerHTML)) * parseInt(num[i].innerHTML);
		}
		
		totalPrice = totalPrice.toFixed(2);
		totalMarket = totalMarket.toFixed(2);
		disparity = totalMarket-totalPrice;
		disparity = disparity.toFixed(2);
		
		if(totalMarket != 0){
			percent = parseFloat(disparity/totalMarket).toFixed(2) * 100;
			percent = percent.toFixed(0);
		}
		
		document.getElementById("priceNotice").innerHTML = "购物金额小计 ￥"+totalPrice+"元，比市场价 ￥"+totalMarket+"元 节省了 ￥"+disparity+"元 ("+percent+"%)";
		document.getElementById("goodsTotal").innerHTML = totalPrice;
		document.getElementById("integrate").innerHTML = parseFloat(totalPrice).toFixed(0);
		
		getUserCoupons('${var_memberid}', totalPrice);
	}
	
	$(function(){
		initTotal();
	});
	
</script>
[#-- 创建购物车列表自定义宏，参数：datalist 购物车列表数据(包含data，total) --]
[#macro ecs_cartlistinfo datalist]
<div class="flowBox">
	<h6>
		<span>商品列表</span><a href="${var_contextpath}/order/cartorder.xhtml" class="f6">修改</a>
	</h6>
	<table width="99%" align="center" border="0" cellpadding="5"
		cellspacing="1" bgcolor="#dddddd">
		<tr>
			<th bgcolor="#ffffff">
				商品名称
			</th>
			<th bgcolor="#ffffff">
				属性
			</th>
			<th bgcolor="#ffffff">
				市场价
			</th>
			<th bgcolor="#ffffff">
				本店价
			</th>
			<th bgcolor="#ffffff">
				购买数量
			</th>
			<th bgcolor="#ffffff">
				小计
			</th>
		</tr>
		[#list datalist.data as data]
		<tr>
			<td bgcolor="#ffffff">
				<a href="goods.xhtml?id=8" target="_blank" class="f6">${data.bciName}</a>
			</td>
			<td bgcolor="#ffffff">
				${data.bciAttrname}
			</td>
			<td align="right" bgcolor="#ffffff">
				￥<label name="marketPrice">${data.bciMarket+data.bciAttprice}</label>元
			</td>
			<td bgcolor="#ffffff" align="right">
				￥<label name="goodsPrice">${data.bciPrice+data.bciAttprice}</label>元
			</td>
			<td bgcolor="#ffffff" align="right">
				<label name="goodsNum">${data.bciNumber?string('0')}</label>
			</td>
			<td bgcolor="#ffffff" align="right">
				￥<label name="totalPrice">${data.bciTotal}</label>元
			</td>
		</tr>
		[/#list]
		<tr>
			<td bgcolor="#ffffff" colspan="7">
				<label id="priceNotice"></label>
			</td>
		</tr>
	</table>
</div>
[/#macro]